<?php 

class EvaluationModel extends CI_Model {
    public $listeQuestion;
    public $formation;
    public $typeEval;
    public $listeModuleAll;
    public $listeSemestre;
    
    function EvaluationModel()
    {
        // Call the Model constructor
       // parent::Model();
    }    
    
    function getData()
    {        
        return array(            
            'listeQuestion'=>$this->listeQuestion ,
            'formation'=>$this->formation, 
            'typeEval'=>$this->typeEval,
            'listeModuleAll'=>$this->listeModuleAll,
            'listeSemestre'=>$this->listeSemestre
        );
    }
    
   function getListeQuestion($type)
   {      
       $this->typeEval=$type;
       $idmodule="";
       if(empty($_POST["lstFormation"]))
       {
           $idmodule=$this->session->userdata('idmodule');
       }
       else
       {
          $idmodule=$_POST["lstFormation"]; 
       }
       
	   
        $query1= $this->db->query("call getSemestre(2015,2025)");    
        $this->listeSemestre = $query1->result();  
        $query1->next_result(); // Dump the extra resultset.
        $query1->free_result();          
       
	   
	   
       $this->db->select('nom,idmodule');
       $this->db->from('module');    
       $this->listeModuleAll = $this->db->get();
       
       
       
       $this->db->select('idquestion,Nomquestion,typequestion,module_has_questions.ordre,Categquestion');
       $this->db->from('module_has_questions'); 
       $this->db->join('questions','questions.idquestion=module_has_questions.questions_idquestions'); 
       $this->db->where('module_idmodule',$idmodule);
       $this->db->where('Categquestion',$type);
       $this->db->order_by('module_has_questions.ordre','asc');
       $this->db->order_by('idquestion','asc');
       $this->listeQuestion = $this->db->get();
       return $this->listeQuestion->num_rows();       
   }
   
   function getFormation()
   {
      $idmodule="";
       if(empty($_POST["lstFormation"]))
       {
           $idmodule=$this->session->userdata('idmodule');
       }
       else
       {
          $idmodule=$_POST["lstFormation"]; 
       }
       $this->db->select('idmodule,nom');
      $this->db->from('module'); 
      $this->db->where('idmodule',$idmodule);      
      $this->formation =  $this->db->get()->result();   
   }
   
   function enregistrerQuestion()
   {
	   
       foreach($_POST as $k => $v)
       {
           if(stripos ($k,"question_")!==False && substr($k,strlen($k)-2,2) != "_b")
           {
               $tabQ=explode("_",$k);
               $reponseTexte="";
               $reponseChoix="";
               
               if($tabQ[2]==1 || $tabQ[2]==4)
               {               
                $reponseChoix=$v;
               }
               else if($tabQ[2]==2 || $tabQ[2]==3)
               {                
                $reponseTexte=$v; 
               }
               else if ($tabQ[2]==5)
               {
                  if($tabQ[3]=="a")
                  {
                     $reponseChoix=$v; 
                     if ($v=="1")
                     {
                        $reponseTexte=$_POST[$tabQ[0]."_".$tabQ[1]."_".$tabQ[2]."_b"];
                     }
                  }                  
               }
               else if($tabQ[2]==6)
               {
                   $vals="";
                   foreach ($v as $value)
                   {
                       $vals.=$value.";";
                   }
                   if ($vals != "") 
                   {
                       $vals = substr($vals, 0,  strlen($vals)-1);
                   }
                   $reponseTexte=$vals;
                   
               }
               else if($tabQ[2]==7)
               {
                   $reponseTexte=$vals;
               }
               
               $data = array(
                    'idquestion'=>$tabQ[1],
                    'iduser'=>$this->session->userdata('id'),
                    'idmodule'=>$_POST["idmodule"],
                    'date'=> date('Y-m-d'),
                    'reponseTexte'=>$reponseTexte,
                    'reponseChoix'=>$reponseChoix
                );
			   
               $this->db->insert('resultateval',$data);
           }               
       }
	   
   }
   
    
}
